package com.robot.common.result;

import com.robot.common.enums.ResponseEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param <T>
 */
@Data
public class Response<T> implements Serializable {

    /**
     * 响应编码
     */
    private Integer code;
    /**
     * 消息内容
     */
    private String msg;
    /**
     * 响应数据
     */
    private T data;

    private Response() {
    }

    private enum Singleton{
        INSTANCE;
        private Response response;
        Singleton(){
            if (response == null){
                response = new Response();
            }
        }
        public Response getInstance(){
            return response;
        }
    }

    public static <T> Response<T> success() {
        return success(null);
    }

    public static <T> Response<T> success(T data) {
        Response<T> responseVo = Singleton.INSTANCE.getInstance();
        responseVo.setCode(ResponseEnum.SUCCESS.getCode());
        responseVo.setMsg(ResponseEnum.SUCCESS.getMessage());
        responseVo.setData(data);
        return responseVo;
    }

    public static <T> Response<T> error(Integer code, String msg) {
        Response<T> responseVo = Singleton.INSTANCE.getInstance();
        responseVo.setCode(code);
        responseVo.setMsg(msg);
        responseVo.setData(null);
        return responseVo;
    }

    public static <T> Response<T> error(String msg) {
        Response<T> responseVo = Singleton.INSTANCE.getInstance();
        responseVo.setCode(ResponseEnum.ERROR.getCode());
        responseVo.setMsg(ResponseEnum.ERROR.getMessage());
        responseVo.setData(null);
        return responseVo;
    }


}
